package com.base.system.base.db.orders;

import cn.hutool.core.util.StrUtil;

import javax.persistence.criteria.*;

public interface OrderBy {

    JoinType joinType = JoinType.INNER; //默认是INNER

    /**
     * 获取需要查询的属性
     */
    default String getProperty(String property) {
        if (property.contains(".")) {
            String[] propertyAttr = StrUtil.split(property, ".");
            return propertyAttr[propertyAttr.length - 1];
        }
        return property;
    }

    /**
     * 获取Path，需要对比的字段的From
     */
    default From getFrom(String property, Root root) {
        From from = root;
        if (property.contains(".")) {
            String[] propertyAttr = StrUtil.split(property, ".");
            for (int i = 0; i < propertyAttr.length - 1; i++) from = from.join(propertyAttr[i], joinType);
        }
        return from;
    }

    Order toOrder(Root root, CriteriaQuery cq, CriteriaBuilder cb);
}
